/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.pdx.cs.guavadata.query;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author VuTran
 */
public class SqlText {
    public String command = "";
    public List<String> projectColumns = new ArrayList<String>();
    public List<String> projectAliases = new ArrayList<String>();
    public String from = "";
    public String where = "";

    @Override
    public String toString() {
        if (!command.equals(""))
            return command;
        if (from.equals("")) {
            // we have a row constant
            String retval = "VALUES (";
            for (String p : projectColumns) 
                retval = retval + p + ", ";
            return retval.substring(0, retval.length() - 2) + ")";
        }
        
        return "SELECT " + getSelect() + " FROM " + from + ((where == "") ? "" : " WHERE " + where);
    }

    public String toUpdateString(String table, Collection<String> columns) {
        try {
            if (!command.equals(""))
                throw new Exception("Cannot create an update string with either a command or an empty from list");

        } catch (Exception e) {
            System.err.println("ERROR: " + e.toString());
        }
        String build = "";
        List<String> setColumns = new ArrayList<String>(columns);
        for (int i = 0; i < projectColumns.size(); i++) {
            build = build + setColumns.get(i) + "=" +
                    (projectColumns.get(i).equals("") ? projectAliases.get(i) : projectColumns.get(i)) + ", ";
        }
        build = build.substring(0, build.length() - 2);

        return "UPDATE " + table + " SET " + build + ((from.equals("")) ? "" : " FROM " + from) +
                ((where.equals("")) ? "" : " WHERE " + where);
    }

    public void clearProjection() {
        this.projectAliases.clear();
        this.projectColumns.clear();
    }

    public String get(String s) throws Exception {
        if (this.projectAliases.contains(s))
            return this.projectColumns.get(this.projectAliases.indexOf(s));
        else
            throw new Exception("Element not found");
    }

    public void set(String s, String value) {
        if (this.projectAliases.contains(s))
            this.projectColumns.set(this.projectAliases.indexOf(s), value);
        else {
            this.projectAliases.add(s);
            this.projectColumns.add(value);
        }
    }

    public String getAlias(String s) throws Exception {
        if (this.projectColumns.contains(s))
            return this.projectAliases.get(projectColumns.indexOf(s));
        else
            throw new Exception("Element not found");
    }

    public void add(String alias, String value) {
        this.projectAliases.add(alias);
        this.projectColumns.add(value);
    }

    public void remove(String s) {
        this.projectColumns.remove(this.projectAliases.indexOf(s));
        this.projectAliases.remove(s);
    }

    private String getSelect() {
        if (projectAliases.size() <= 0) 
            return "*";
        
        String build = "";
        for (int i = 0; i < projectColumns.size(); i++) {
            build = build + (projectColumns.get(i).equals("") ? "" : (projectColumns.get(i) +
                    " AS ")) + projectAliases.get(i) + ", ";
        }
        return build.substring(0, build.length() - 2);
    }
}
